// pages/son/Course_grade.js
const app = getApp();
const gpaCalculator = require('../../utils/util.js');
var lessonIndex=0;
var bixiuGpa=0;
var Gpa=0;
var lesson_period = [];
var indexCache=0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: [9999, 1, 1],

    show: false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['课程时间 ：', '2', '3', '4', '5', '6','6'],//下拉列表的数据
    index: 0,//选择的下拉列表下标
    //下拉框

    wlist:[],
    animation1:{},
    animation2:{},
    hidden:[]
  },
  
  
  /**
   * 生命周期函数--监听页面加载
   */
  back: function () {
    wx.navigateBack({
      url: '/pages/index/index'
    })
  },
  onLoad: function (options) {
    
    this.setData({
      gpa:app.globalData.gpa,
      total_credit:app.globalData.total_credit,
      total_courses:app.globalData.total_courses,
      average_grade:app.globalData.average_grade
    })
    wx.setNavigationBarTitle({
      title: '成绩查询'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#32B188',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })//页面初始化样式
    
    function unique(arr) {
      var result = [];
      for (var i = 0; i < arr.length; i++) {
        if (result.indexOf(arr[i]) == -1) {
          result.push(arr[i])
        }
      }
      return result;
    }
    //unique备用函数

    var lesson_result = {};
    var lesson_result_Array = [];
    var lesson_result_2D_Array=new Array();
    

    
    for (let i = 0; i < app.globalData.Course_Grade.length; i++) {
     // for (let j = 0; j < app.globalData.Course_Grade[i].lessons_time.length; j++) {

        lesson_result = {
          "name": app.globalData.Course_Grade[i].name,
          "grade": app.globalData.Course_Grade[i].grade,
          "instructor": app.globalData.Course_Grade[i].instructor,
          "type": app.globalData.Course_Grade[i].course_type,
          "credit": app.globalData.Course_Grade[i].credit,
          "year": app.globalData.Course_Grade[i].year,
          "semester": app.globalData.Course_Grade[i].semester
        }
        lesson_result_Array.push(lesson_result);

        if(app.globalData.Course_Grade[i].semester==0){
        lesson_period.push(app.globalData.Course_Grade[i].year+'年上学期');
          lesson_result.semester ='上学期';
        }
        if (app.globalData.Course_Grade[i].semester == 1){
        lesson_period.push(app.globalData.Course_Grade[i].year + '年下学期');
          lesson_result.semester = '下学期';
        }
      //}
    }
    lesson_period = unique(lesson_period);
    lesson_period.sort();

    for (let i = 0; i <lesson_period.length;i++){
      lesson_result_2D_Array[i] = new Array();    //在声明二维数组
    }

    for (let i = 0; i < lesson_result_Array.length; i++){
    for (let j = 0; j < lesson_period.length; j++) {
      if (lesson_period[j] == lesson_result_Array[i].year + '年' + lesson_result_Array[i].semester){
      lesson_result_2D_Array[j].push(lesson_result_Array[i]);
    }
    }
  }
    app.globalData.two_Array=lesson_result_2D_Array;
    
    for (let i = 0; i < lesson_result_2D_Array.length;i++){
      lesson_result_2D_Array[i].unshift([lesson_period[i]]);
    }
    var cache=new Array(1);
    cache[0]=0;
    for (let i = 1; i < lesson_period.length; i++) {
      cache.push(0);
      this.setData({
        hidden: cache
      })
    }
    this.setData({
      term: lesson_period[0]
    })

    let cache5 = 0;
    let cache6 = 0;
    let cache7 = 0;
    let cache8 = 0;

    for (let i = 0; i < lesson_result_2D_Array.length; i++){
      let cache1=0;
      let cache2=0;
      let cache3=0;
      let cache4=0;
      for (let j = 1; j < lesson_result_2D_Array[i].length;j++){
        if (lesson_result_2D_Array[i][j].grade == -1) {
          lesson_result_2D_Array[i][j].grade=0;
        }
        if (lesson_result_2D_Array[i][j].type.search("必修") != -1 && lesson_result_2D_Array[i][j].grade != -1 && lesson_result_2D_Array[i][j].grade != 0){
        let gpaCache=gpaCalculator.GradetoGpa(lesson_result_2D_Array[i][j].grade);
          cache1 += (gpaCache*1.0) * (lesson_result_2D_Array[i][j].credit*1.0);
          cache5 += (gpaCache * 1.0) * (lesson_result_2D_Array[i][j].credit * 1.0);
          cache2 += lesson_result_2D_Array[i][j].credit*1.0;
          cache6 += lesson_result_2D_Array[i][j].credit * 1.0;
        }
        if (lesson_result_2D_Array[i][j].grade != -1 && lesson_result_2D_Array[i][j].grade != 0){
        let gpaCache2 = gpaCalculator.GradetoGpa(lesson_result_2D_Array[i][j].grade);
        cache3 += gpaCache2 * lesson_result_2D_Array[i][j].credit;
        cache7 += gpaCache2 * lesson_result_2D_Array[i][j].credit;
        cache4 += lesson_result_2D_Array[i][j].credit;
        cache8 += lesson_result_2D_Array[i][j].credit;
        }
      }
      if (cache2 == 0) cache2 = 1;
      if (cache4 == 0) cache4 = 1;
      let averageGpaBixiu=cache1/cache2;
      averageGpaBixiu = averageGpaBixiu.toFixed(2);
      let averageGpa =cache3/cache4;
      averageGpa = averageGpa.toFixed(2);
      if (averageGpaBixiu == NaN || averageGpaBixiu==null) averageGpaBixiu=0;
      if (averageGpa == NaN || averageGpa == null) averageGpa = 0;
      lesson_result_2D_Array[i][0].push(averageGpaBixiu);//必修gpa
      lesson_result_2D_Array[i][0].push(averageGpa);//gpa
    }
    
    bixiuGpa=cache5/cache6;
    Gpa = cache7 / cache8;
    this.setData({
      wlist: lesson_result_2D_Array,
      bixiuGpa: bixiuGpa.toFixed(3),
      gpa: Gpa.toFixed(3)
    })
  },
  closed:function(e){

    indexCache=e.currentTarget.dataset.index
    let cache = this.data.hidden;
    if (cache[indexCache] == 0) {
      cache[indexCache] = 1;
    }
    else {
      cache[indexCache] = 0;
    }
    this.setData({
      hidden: cache
    })
  },
 
  selectTap() {
    
  },
  /*optionTap(e) {
    let Index = e.currentTarget.dataset.index-1;//获取点击的下拉列表的下标
    if (Index!=-1){
    this.setData({
      index: Index,
      show: !this.data.show,
      wlist: app.globalData.two_Array[Index],
      term:lesson_period[1]
    });
    }
    else if(Index==-1){
      this.setData({
        term:lesson_period.slice(1,lesson_period.length),
        show: !this.data.show,
        wlist: app.globalData.two_Array,
      });
    }
  },*/
  /*
  showCardView:function(e){
    let animation1 = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 500,
      timingFunction: "ease-out",
      delay: 0
    });
    let animation2 = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 500,
      timingFunction: "ease-out",
      delay: 0
    });
    animation1.height(110).backgroundColor('#6CC7AA').step();
    
    let index = e.currentTarget.dataset.index;
    this.data.wlist.forEach((item, index) => {
      if(index!=this.data.wlist.length-1){
        animation2.height(70).backgroundColor('#FFFFFF').step();
        item.ani = animation2.export();
        item.c='black';
      }
    })
    this.data.wlist[index].ani = animation1.export();
    this.data.wlist[index].c='white';
    this.setData({
      wlist: this.data.wlist,
    })

  },
  */

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})